package com.atguigu.blog.controller;

import com.atguigu.blog.vos.UserVO;
import com.atguigu.blog.constant.SessionConstant;
import com.atguigu.blog.pojos.User;
import com.atguigu.blog.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author CF
 * @date 2023/8/19 11:22
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public UserVO login(@RequestBody User user, HttpSession session){
        //1.调用业务层方法进行登录
        User responseUser = userService.doLogin(user);
        //2.多做一件事情，使用session保存登录成功的用户信息
        session.setAttribute(SessionConstant.LOGIN_SESSION_KEY, responseUser);
        //3.向前端返回一个UserVO对象
        UserVO userVO = new UserVO();
        userVO.setUid(responseUser.getUid());
        userVO.setName(responseUser.getUsername());
        return userVO;
    }
    @GetMapping("/getUser/{id}")
    public User getUser(@PathVariable("id") Integer id) {
        return userService.findById(id);
    }
    @PutMapping("/updateUser")
    public boolean updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }
}
